package com.dinadi.yidi.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.MyCouponsStateRequestEntity;
import com.dinadi.yidi.utils.FormatCurrentData;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/7/14
 * Time: 10:49
 */
public class CouponCenterAdapter2 extends BaseAdapter {

    private Activity mContext;
    private List<MyCouponsStateRequestEntity> mDatas = new ArrayList<>();

    public CouponCenterAdapter2(Activity context, List<MyCouponsStateRequestEntity> mList) {
        this.mContext = context;
        this.mDatas = mList;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.adapter_my_coupons_state_item, null); /**这个是评论的item列表*/
            viewHolder = new ViewHolder();
            viewHolder.tv_coupon_name = (TextView) convertView.findViewById(R.id.tv_coupon_name);
            viewHolder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
            viewHolder.tv_full_account = (TextView) convertView.findViewById(R.id.tv_full_account);
            viewHolder.term_of_validity = (TextView) convertView.findViewById(R.id.term_of_validity);
            viewHolder.item_layout = (RelativeLayout) convertView.findViewById(R.id.item_layout);
            viewHolder.icon_coupons_available = (ImageView) convertView.findViewById(R.id.icon_coupons_available);
            viewHolder.coupon_full_court = (ImageView) convertView.findViewById(R.id.coupon_full_court);
            viewHolder.icon_conpous_already_in_use = (ImageView) convertView.findViewById(R.id.icon_conpous_already_in_use);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv_coupon_name.setText(mDatas.get(position).getTitle());
        viewHolder.tv_money.setText("￥" + mDatas.get(position).getAccount());
        viewHolder.tv_full_account.setText("满" + mDatas.get(position).getFullAccount() + "可用");
        String startDate = FormatCurrentData.getDate(mDatas.get(position).getEffectiveTime());
        String endDate = FormatCurrentData.getDate(mDatas.get(position).getExpirationTime());
        viewHolder.term_of_validity.setText(startDate + " - " + endDate);
        String isReceive = mDatas.get(position).getIsReceive();
        String isReceiveTime = mDatas.get(position).getIsReceiveTime();
        String id = mDatas.get(position).getId();
        if ("0".equals(mDatas.get(position).getIsReceive())) {
            /*已领取*/
            viewHolder.icon_conpous_already_in_use.setVisibility(View.VISIBLE);
            viewHolder.icon_conpous_already_in_use.setImageResource(R.drawable.img_already_received);
            viewHolder.icon_coupons_available.setImageResource(R.drawable.icon_coupons_unavailable);
            viewHolder.coupon_full_court.setImageResource(R.drawable.icon_coupon_full_court_no);
        }
        if("2" .equals(mDatas.get(position).getIsNoStock())){
            /*已领完*/
            viewHolder.icon_conpous_already_in_use.setVisibility(View.VISIBLE);
            viewHolder.icon_conpous_already_in_use.setImageResource(R.drawable.img_have_been_completed);
            viewHolder.icon_coupons_available.setImageResource(R.drawable.icon_coupons_unavailable);
            viewHolder.coupon_full_court.setImageResource(R.drawable.icon_coupon_full_court_no);
        }
        viewHolder.item_layout.setOnClickListener(new AddressManagementAdapterOnClickListener(position));


        return convertView;
    }


    class ViewHolder {
        TextView tv_coupon_name, tv_money, tv_full_account, term_of_validity;
        RelativeLayout item_layout;
        ImageView icon_coupons_available, coupon_full_court, icon_conpous_already_in_use;


    }

    private class AddressManagementAdapterOnClickListener implements View.OnClickListener {
        int position;

        public AddressManagementAdapterOnClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.item_layout:

                    onClickListenerCallBack.onClickDefault(position, mDatas.get(position).getIsReceive(), mDatas.get(position).getIsReceiveTime(),mDatas.get(position).getId(),mDatas.get(position).getIsNoStock());

                    break;

            }
        }
    }

    public interface OnClickListenerCallBack {
        void onClickDefault(int position, String isReceive, String isReceiveTime,String id,String isNoStock);

    }

    public OnClickListenerCallBack onClickListenerCallBack;

    public void setOnClickListenerCallBack(OnClickListenerCallBack onClickListenerCallBack) {
        this.onClickListenerCallBack = onClickListenerCallBack;
    }
}

